package com.woniuxy.platform.core.controller;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.common.CommonResult;
import com.woniuxy.platform.core.model.PmMessage;
import com.woniuxy.platform.core.service.PmMessageService;
import com.woniuxy.platform.core.vo.PmMessageVo;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author gmx
 * @since 2021-06-30
 */
@RestController
@RequestMapping("/pmMessage")
public class PmMessageController {

    @Resource
    private PmMessageService pmMessageService;

    @ApiOperation("查看系统发布的公告信息")
    @GetMapping("/selectAnnouncement")
    public CommonResult selectAnnouncement(){

        List<PmMessage> pmMessages = pmMessageService.selectAnnouncement();
        System.out.println(pmMessages);
        return CommonResult.success(pmMessages);
    }

    @ApiOperation("查看系统发布的消息通知")
    @GetMapping("/selectMesNotice")
    public CommonResult selectMesNotice(){
        List<PmMessage> pmMessages  = pmMessageService.selectMesNotice();
        System.out.println(pmMessages);
        return CommonResult.success(pmMessages);
    }

    @ApiOperation("查看系统发布的活动通知")
    @GetMapping("/selectActivityNotice")
    public CommonResult selectActivityNotice(){
        List<PmMessage> pmMessages  = pmMessageService.selectActivityNotice();
        System.out.println(pmMessages);
        return CommonResult.success(pmMessages);
    }


    @GetMapping("selectMessage")
    public CommonResult<PageInfo> selectMessage(int startpage , int pagesize){
        PageHelper.startPage(startpage, pagesize);
        Page<PmMessage> page =(Page<PmMessage>) this.pmMessageService.selectMessage();
        PageInfo<PmMessage> info = new PageInfo<>(page);
        return CommonResult.success(info);
    }

    @PostMapping("selectMH")
    public CommonResult<PageInfo> selectMH(@RequestBody PmMessageVo messageVo){
        System.out.println(messageVo.toString());
        PageHelper.startPage(messageVo.getPage(),messageVo.getLimit());
        Page<PmMessage> page =(Page<PmMessage>) this.pmMessageService.selectMH(messageVo.getMessage());
        PageInfo<PmMessage> pageInfo = new PageInfo<>(page);
        return CommonResult.success(pageInfo);
    }

    @ApiOperation("消息服务")
    @GetMapping("selectAll")
    public CommonResult<PageInfo> selectAll(int startpage , int pagesize){
        PageHelper.startPage(startpage, pagesize);
        Page<PmMessage> page = (Page<PmMessage>) this.pmMessageService.slectAll();
        PageInfo<PmMessage> pageInfo = new PageInfo<>(page);
        return CommonResult.success(pageInfo);
    }


    @PostMapping("selectMHH")
    public CommonResult<PageInfo> selectMHH(@RequestBody PmMessageVo messageVo){
        System.out.println(messageVo.toString());
        PageHelper.startPage(messageVo.getPage(),messageVo.getLimit());
        Page<PmMessage> page =(Page<PmMessage>) this.pmMessageService.selectMHH(messageVo.getMessage());
        PageInfo<PmMessage> pageInfo = new PageInfo<>(page);
        return CommonResult.success(pageInfo);
    }

    @PostMapping("insert")
    public CommonResult insert(@RequestBody PmMessage pmMessage){
        boolean save = this.pmMessageService.save(pmMessage);
        if (save){
            return CommonResult.success();
        }
        return CommonResult.fail();
    }

    @PostMapping("update")
    public CommonResult update(@RequestBody PmMessage pmMessage){
        boolean b = this.pmMessageService.updateById(pmMessage);
        if (b){
            return CommonResult.success();
        }
        return CommonResult.fail();
    }

    @GetMapping("delete/{id}")
    public CommonResult delete(@PathVariable("id") int id){
        boolean b = this.pmMessageService.removeById(id);
        if (b){
            return CommonResult.success();
        }
        return CommonResult.fail();
    }
}

